package com.lv.service.dialog

import android.content.Context
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.lv.common.imp.OnPositionMore
import com.lv.common.imp.OnPositionTListener
import com.lv.common.imp.OnPositionTMore
import com.lv.common.utils.AnimationUtils
import com.lv.common.utils.MoneyUtil
import com.lv.common.utils.showToast
import com.lv.service.R
import com.lv.service.bean.CommonSelectedBean
import com.lv.service.bean.TelThirdCategory
import com.lv.service.room.category.CategoryBean
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupPosition
import com.lxj.xpopup.impl.PartShadowPopupView

/**
 * 作者： ygx
 * 创建日期：2022/10/10 10:32 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class SendCouponBottomPopupView(
    private val mContext: Context,
    private val mTitle: String = "",
    private val mAddressBeans: List<CommonSelectedBean>?,
    private val mUsedTime: List<CommonSelectedBean>?,
    private val mSendTimes: List<CategoryBean>?,
    private var mWheelSendPosition0: Int = 0,
    private var mWheelSendPosition1: Int = 0,
    private var mWheelSendPosition2: Int = 0,
    private var topH: Int = 0,
    private val positionListener: OnPositionTMore<String>? = null
) :
    PartShadowPopupView(mContext) {
    private var tvTitle: TextView? = null
    private var tvSure: TextView? = null
    private var tvExpiration: TextView? = null
    private var tvSendAddress: TextView? = null
    private var tvSendData: TextView? = null
    private var llTime: LinearLayout? = null
    private var llAddress: LinearLayout? = null
    private var llSendData: LinearLayout? = null
    private var etName: EditText? = null
    private var etPrice: EditText? = null
    private var etPay: EditText? = null
    private var etNumb: EditText? = null
    private var viewTop: View? = null
    private var mName = "速易甬平台现金券"//现金券名称
    private var mPrice = ""//金额
    private var mPayPrice = ""//实付金额
    private var mNum = ""//数量
    private var mExpiration = ""//有效期
    private var mExpirationDay = ""//有效期
    private var mSendAddress = ""//发券地区
    private var mSendData = ""//发券日期
    override fun getImplLayoutId(): Int {
        return R.layout.send_coupon_bottom_popup
    }

    override fun onCreate() {
        super.onCreate()
        viewTop = findViewById<View>(R.id.view_top)
        tvTitle = findViewById<TextView>(R.id.tv_title)
        tvSure = findViewById<TextView>(R.id.tv_sure)
        tvExpiration = findViewById<TextView>(R.id.tv_expiration)
        tvSendAddress = findViewById<TextView>(R.id.tv_send_address)
        tvSendData = findViewById<TextView>(R.id.tv_send_data)
        llTime = findViewById<LinearLayout>(R.id.ll_time)
        llAddress = findViewById<LinearLayout>(R.id.ll_address)
        llSendData = findViewById<LinearLayout>(R.id.ll_send_data)
        etName = findViewById<EditText>(R.id.et_name)
        etPrice = findViewById<EditText>(R.id.et_price)
        etPay = findViewById<EditText>(R.id.et_pay)
        etNumb = findViewById<EditText>(R.id.et_numb)
//        etPrice?.addTextChangedListener(DecimalInputTextWatcher(etPrice, 10, 2))
//        etPay?.addTextChangedListener(DecimalInputTextWatcher(etPay, 10, 2))

        val layoutParams = viewTop?.layoutParams as ConstraintLayout.LayoutParams
        layoutParams.height = topH
        initView()
        initEvnet()
    }

    private fun initView() {
        tvTitle?.text = mTitle
        etName?.setText(mName)
        mSendData = getSendTime()
        tvSendData?.text = mSendData
        mExpiration = mUsedTime?.get(0)?.name ?: ""
        mExpirationDay = mUsedTime?.get(0)?.tag ?: ""
        tvExpiration?.text = mExpiration
        mSendAddress = mAddressBeans?.get(0)?.name ?: ""
        tvSendAddress?.text = mSendAddress
        etName?.setSelection(mName.length)
        etPrice?.setOnLongClickListener { true }
        etPay?.setOnLongClickListener { true }
        etName?.setOnLongClickListener { true }
        etNumb?.setOnLongClickListener { true }
//        etPrice?.isFocusable=true
//        etPrice?.isFocusableInTouchMode = true
//        etPrice?.requestFocus()
    }

    private fun initEvnet() {
//        etName?.addTextChangedListener(object : TextWatcher {
//            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
//
//            }
//
//            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
//            }
//
//            override fun afterTextChanged(s: Editable?) {
//            }
//        })

        viewTop?.setOnClickListener {
            dismiss()
        }
        findViewById<ImageView>(R.id.iv_close).setOnClickListener {
            dismiss()
        }
        llTime?.setOnClickListener {
            showTimeUsedPop()
        }
        llAddress?.setOnClickListener { showAddressPop() }
        llSendData?.setOnClickListener { showSendTimePop() }
        tvSure?.setOnClickListener {
            mName = etName?.text.toString().trim()
            if (mName.isEmpty()) {
                AnimationUtils.setHintAnimation(etName)
                showToast("请输入现金券名称")
                return@setOnClickListener
            }
            mPrice = etPrice?.text.toString().trim()
            if (mPrice.isEmpty()) {
                AnimationUtils.setHintAnimation(etPrice)
                showToast("请输入金额")
                return@setOnClickListener
            }
            mPayPrice = etPay?.text.toString().trim()
            if (mPayPrice.isEmpty()) {
                AnimationUtils.setHintAnimation(etPay)
                showToast("请输入实付金额")
                return@setOnClickListener
            }
            val moneyComp = MoneyUtil.moneyComp(mPayPrice, mPrice)
            if (moneyComp && mPayPrice != mPrice) {
                showToast("实付金额需大于金额")
                return@setOnClickListener
            }
            mNum = etNumb?.text.toString().trim()
            if (mNum.isEmpty()) {
                AnimationUtils.setHintAnimation(etNumb)
                showToast("请输入数量")
                return@setOnClickListener
            }
            positionListener?.onClick(
                0,
                mName, mPrice, mPayPrice, mNum, mExpirationDay, mSendAddress, mSendData
            )
//            dismiss()
        }
    }

    private fun showAddressPop() {
        if (mAddressBeans.isNullOrEmpty()) {
            return showToast("地址数据有误，请退出重试")
        }
        val popupView = RecyclerSelectedTagTopHPopupView(mContext, mAddressBeans, "发券地区", topH,
            mPositionTListener = OnPositionTListener<CommonSelectedBean> { position, data ->
                mSendAddress = mAddressBeans[position].name ?: ""
                tvSendAddress?.text = mSendAddress
            })
        XPopup.Builder(mContext)
            .isViewMode(true)
            .hasShadowBg(false)
            .popupPosition(PopupPosition.Bottom)
            .asCustom(popupView)
            .show()
    }

    private fun showTimeUsedPop() {
        val popupView = RecyclerSelectedTagTopHPopupView(mContext, mUsedTime, "有效期", topH,
            mPositionTListener = OnPositionTListener<CommonSelectedBean> { position, data ->
                mExpiration = mUsedTime?.get(position)?.name ?: ""
                mExpirationDay = mUsedTime?.get(position)?.tag ?: ""

                tvExpiration?.text = mExpiration
            })
        XPopup.Builder(mContext)
            .isViewMode(true)
            .hasShadowBg(false)
            .popupPosition(PopupPosition.Bottom)
            .asCustom(popupView)
            .show()
    }

    private fun showSendTimePop() {
        val popupView =
            CategoryBottom3PopupView(
                mContext,
                "时间选择",
                mSendTimes,
                mWheelSendPosition0,
                mWheelSendPosition1,
                mWheelSendPosition2,
                -1,
                object : OnPositionMore<TelThirdCategory> {
                    override fun onClick(vararg args: Int, data: TelThirdCategory?) {
                        mWheelSendPosition0 = args[0]
                        mWheelSendPosition1 = args[1]
                        mWheelSendPosition2 = args[2]
                        mSendData = getSendTime()
                        tvSendData?.text = mSendData
                    }
                })
        XPopup.Builder(mContext)
            .isViewMode(true)
            .popupPosition(PopupPosition.Bottom)
            .asCustom(popupView)
            .show()
    }

    private fun getSendTime(): String {
        val year = mSendTimes?.get(mWheelSendPosition0)?.name
        val mouth =
            mSendTimes?.get(mWheelSendPosition0)?.telSecondCategories?.get(mWheelSendPosition1)?.name
        val day =
            mSendTimes?.get(mWheelSendPosition0)?.telSecondCategories?.get(mWheelSendPosition1)?.telThirdCategories?.get(
                mWheelSendPosition2
            )?.name
        return "$year-$mouth-$day"
    }


    override fun onDestroy() {
        super.onDestroy()
    }

    override fun beforeDismiss() {
        super.beforeDismiss()

    }


}